package com.mall.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.mall.entity.*;
import com.mall.service.OrderService;
import com.mall.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 购物车结算
     */
    @PostMapping("/settlement")
    public WebResult<JSONObject> settlement(@RequestBody SettlementVo settlementVo) {
        JSONObject res = orderService.settlement(settlementVo);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .data(res)
                .build();
    }

    /**
     * 下单操作
     * @param submitOrderVo
     */
    @PostMapping("/submitOrder")
    public WebResult<JSONObject> submitOrder(@RequestBody SubmitOrderVo submitOrderVo) {
        JSONObject res = orderService.submitOrder(submitOrderVo);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .data(res)
                .build();
    }


    /**
     * 查询当前登录用户的订单
     * @param userId 当前登录用户的id
     */
    @PostMapping("/orderList")
    public WebResult<JSONObject> orderList(Integer userId) {
        JSONObject res = orderService.queryOrderList(userId);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .data(res)
                .build();
    }


    /**
     * 用户查询订单详情
     * @param userId  当前登录用户的id
     * @param orderId 当前订单的编号
     */
    @PostMapping("/orderView")
    public WebResult<JSONObject> orderView(Integer userId, String orderId) {
        JSONObject res = orderService.queryOrderList(userId);
        List<Order> allOrders = (List<Order>) res.get("allOrders");
        for (Order order : allOrders) {
            if (orderId.equals(order.getOrderId())) {
                JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
                jsonObject.put("order", order);
                return WebResult.<JSONObject>builder()
                        .code(10000)
                        .message(WebResult.REQUEST_STATUS_SUCCESS)
                        .data(jsonObject)
                        .build();
            }
        }
        return null;
    }


    /**
     * 修改订单的收货信息
     * @param orderId       当前订单的编号
     * @param updateAddress 需要修改的收货信息
     */
    @PostMapping("/updateAddress")
    public WebResult<JSONObject> updateAddress(String orderId, UpdateAddress updateAddress) {
        orderService.updateAddress(orderId, updateAddress);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .build();
    }


    /**
     * 修改订单状态为已付款
     */
    @PostMapping("/updatePayed")
    public WebResult<JSONObject> updatePayed(String orderId) {
        int status = OrderStatusEnum.PAYED.getCode();
        orderService.updateStatus(orderId, status);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .build();
    }


    /**
     * 修改订单状态为已发货
     */
    @PostMapping("/updateSended")
    public WebResult<JSONObject> updateSended(String orderId) {
        int status = OrderStatusEnum.SENDED.getCode();
        orderService.updateStatus(orderId, status);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .build();
    }


    /**
     * 修改订单状态为已完成
     */
    @PostMapping("/updateReceived")
    public WebResult<JSONObject> updateReceived(String orderId) {
        int status = OrderStatusEnum.RECEIVED.getCode();
        orderService.updateStatus(orderId, status);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .build();
    }


    /**
     * 修改订单状态为已取消
     */
    @PostMapping("/updateCancled")
    public WebResult<JSONObject> updateCancled(String orderId) {
        int status = OrderStatusEnum.CANCLED.getCode();
        orderService.updateStatus(orderId, status);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .build();
    }

    /**
     * 修改订单状态为售后中
     */
    @PostMapping("/updateServicing")
    public WebResult<JSONObject> updateServicing(String orderId) {
        int status = OrderStatusEnum.SERVICING.getCode();
        orderService.updateStatus(orderId, status);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .build();
    }


    /**
     * 修改订单状态为售后完成
     */
    @PostMapping("/updateServiced")
    public WebResult<JSONObject> updateServiced(String orderId) {
        int status = OrderStatusEnum.SERVICED.getCode();
        orderService.updateStatus(orderId, status);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .build();
    }


    /**
     * 管理员根据订单编号查询全部订单
     * @param orderId     订单编号
     * @param currentPage 当前页码
     * @param pageSize    页面大小
     */
    @PostMapping("/queryAllOrder")
    public WebResult<JSONObject> queryAllOrder(String orderId, Integer currentPage, Integer pageSize) {
        List<TableData> tableData = orderService.queryAllOrder(orderId, currentPage, pageSize);
        //查询订单总条数
        Long total = orderService.queryCount(orderId);
        //构造分页信息
        PageInfo pagination = new PageInfo(currentPage, pageSize, total);
        JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
        jsonObject.put("tableData", tableData);
        jsonObject.put("pagination", pagination);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .data(jsonObject)
                .build();
    }


    /**
     * 管理员根据订单编号删除订单
     */
    @PostMapping("/deleteOrder")
    public WebResult<JSONObject> deleteOrder(String orderId) {
        orderService.deleteOrder(orderId);
        return WebResult.<JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .build();
    }


    /**
     * 管理员根据订单编号查询订单
     */
    @PostMapping("/queryOrder")
    public WebResult<JSONObject> queryOrder(String orderId) {
        TableData currentEditOrder = orderService.queryOrder(orderId);
        JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
        jsonObject.put("currentEditOrder", currentEditOrder);
        return WebResult.<com.alibaba.fastjson.JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .data(jsonObject)
                .build();
    }


    /**
     * 管理员根据订单编号修改订单
     */
    @PostMapping("/updateOrder")
    public WebResult<JSONObject> updateOrder(String orderId, Integer status, String createTime) {
        orderService.updateOrder(orderId, status, createTime);
        return WebResult.<com.alibaba.fastjson.JSONObject>builder()
                .code(10000)
                .message(WebResult.REQUEST_STATUS_SUCCESS)
                .build();
    }



    /**
     * 管理员查询订单详情
     */
    @PostMapping("/viewDetails")
    public WebResult<JSONObject> viewDetails(String orderId) {
        Integer userId = orderService.queryUserId(orderId);
        System.out.println(userId);
        JSONObject res = orderService.queryOrderList(userId);
        List<Order> allOrders = (List<Order>) res.get("allOrders");
        for (Order order : allOrders) {
            if (orderId.equals(order.getOrderId())) {
                JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
                jsonObject.put("order", order);
                return WebResult.<JSONObject>builder()
                        .code(10000)
                        .message(WebResult.REQUEST_STATUS_SUCCESS)
                        .data(jsonObject)
                        .build();
            }
        }
        return null;
    }





}